package com.zhw.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhw.mapper.ReportMapper;
import com.zhw.pojo.PageBean;
import com.zhw.pojo.dto.AmountDTO;
import com.zhw.pojo.dto.CustomerConstituteDTO;
import com.zhw.pojo.dto.CustomerLossQueryDTO;
import com.zhw.pojo.vo.*;
import com.zhw.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    ReportMapper reportMapper;

    @Override
    public PageBean getAmount(AmountDTO amountDTO) {
        PageHelper.startPage(amountDTO.getPage(),amountDTO.getPageSize());
        Page<AmountVO> page=reportMapper.getAmount(amountDTO);

        return new PageBean(page.getTotal(),page.getResult());


    }


    @Override
    public CustomerConstituteVO getCustomerConstitute() {
        List<CustomerConstituteDTO> list=reportMapper.getCustomerConstitute();

        StringJoiner level=new StringJoiner(",");
        StringJoiner total=new StringJoiner(",");
        List<CustomerConstituteRecord> records=new ArrayList<>();

        for (CustomerConstituteDTO customerConstituteDTO : list) {
            level.add(customerConstituteDTO.getLevel());
            total.add(String.valueOf(customerConstituteDTO.getTotal()));
//            { value: 335, name: 'Direct' },
            CustomerConstituteRecord customerConstituteRecord=new CustomerConstituteRecord();
            customerConstituteRecord.setName(customerConstituteDTO.getLevel());
            customerConstituteRecord.setValue(customerConstituteDTO.getTotal());
            records.add(customerConstituteRecord);
        }



        return new CustomerConstituteVO(level.toString(),total.toString(),records);
    }

    @Override
    public CustomerServeReport getServeReport() {
        List<CustomerServeReport> list=reportMapper.getServeReport();

        StringJoiner type=new StringJoiner(",");
        StringJoiner total=new StringJoiner(",");


        for (CustomerServeReport customerServeReport : list) {
            type.add(customerServeReport.getType());
            total.add(customerServeReport.getTotal());
        }

        return new CustomerServeReport(type.toString(),total.toString());
    }


    @Override
    public PageBean getLose(CustomerLossQueryDTO customerLossQueryDTO) {
        PageHelper.startPage(customerLossQueryDTO.getPage(),customerLossQueryDTO.getPageSize());
        Page<CustomerLossReportVO> page=reportMapper.getLose(customerLossQueryDTO);

        return new PageBean(page.getTotal(),page.getResult());

    }
}
